package zzz.tuan2d.thuchi;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import zzz.tuan2d.toidihoilo.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.roomorama.caldroid.CaldroidFragment;
import com.roomorama.caldroid.CaldroidListener;

public class NoteCreateActivity extends BaseActivity {
	private CaldroidFragment dialogCaldroidFragment;
	Date selectedDate;
	Button buttonTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_create);
		initHeader("Thêm mục mới");
		enableCreateButton(false);

		buttonTime = (Button) findViewById(R.id.buttonTime);
		buttonTime.setOnClickListener(onTimeChangeClicked);

		// Setup caldroid to use as dialog
		dialogCaldroidFragment = new CaldroidFragment();
		dialogCaldroidFragment.setCaldroidListener(listener);

		// Setup arguments
		Bundle bundle = new Bundle();
		// Setup dialogTitle
		// bundle.putString(CaldroidFragment.DIALOG_TITLE, "Select a date");
		dialogCaldroidFragment.setArguments(bundle);

		// On save
		findViewById(R.id.buttonSave).setOnClickListener(onSaveClick);
	}

	@Override
	protected void onResume() {
		super.onResume();
		final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		selectedDate = Calendar.getInstance().getTime();
		buttonTime.setText(formatter.format(selectedDate));
	}

	// Setup listener
	final CaldroidListener listener = new CaldroidListener() {

		@Override
		public void onSelectDate(Date date, View view) {
			if (dialogCaldroidFragment != null) {
				if (selectedDate != null) {
					dialogCaldroidFragment.setBackgroundResourceForDate(
							android.R.color.white, selectedDate);
					dialogCaldroidFragment.setTextColorForDate(
							android.R.color.black, selectedDate);
				}
				dialogCaldroidFragment.setBackgroundResourceForDate(
						R.color.brown_text, date);
				dialogCaldroidFragment.setTextColorForDate(
						android.R.color.white, date);
			}
			dialogCaldroidFragment.refreshView();
			selectedDate = date;
			final SimpleDateFormat formatter = new SimpleDateFormat(
					"dd-MM-yyyy");
			buttonTime.setText(formatter.format(selectedDate));
			dialogCaldroidFragment.dismiss();
		}
	};

	OnClickListener onTimeChangeClicked = new OnClickListener() {

		@Override
		public void onClick(View v) {
			dialogCaldroidFragment.show(getSupportFragmentManager(), null);
			if (dialogCaldroidFragment != null && selectedDate != null) {
				dialogCaldroidFragment.setBackgroundResourceForDate(
						R.color.brown_text, selectedDate);
				dialogCaldroidFragment.setTextColorForDate(
						android.R.color.white, selectedDate);
			}
			dialogCaldroidFragment.refreshView();
		}
	};

	OnClickListener onSaveClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int money = Integer
					.parseInt(((EditText) findViewById(R.id.editTextMoney))
							.getText().toString().trim());
			String content = ((EditText) findViewById(R.id.editTextContent))
					.getText().toString().trim();

			insertNewRecord(selectedDate, money, content);
		}
	};

	private void insertNewRecord(Date selectedDate, int money, String content) {
		// TODO Auto-generated method stub

	}
}
